/**
 * 
 */
package com.youth.fund.hk.hostManager.shiro.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.youth.fund.hk.hostManager.common.dao.ICommonDao;
import com.youth.fund.hk.hostManager.common.service.impl.CommonService;
import com.youth.fund.hk.hostManager.shiro.dao.RoleDao;
import com.youth.fund.hk.hostManager.shiro.datamodel.Role;
import com.youth.fund.hk.hostManager.shiro.service.ResourceService;
import com.youth.fund.hk.hostManager.shiro.service.RoleService;

/**
 * @author Sunk_Sky
 *
 *         2014年11月13日下午6:47:11
 */
@Service("RoleService")
public class RoleServiceImpl extends CommonService<Role> implements RoleService
{
	private RoleDao roleDao;

	@Autowired
	@Qualifier("ResourceService")
	private ResourceService resourceService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.youth.fund.hk.hostManager.common.service.impl.CommonService#setCommonDao
	 * (com.youth.fund.hk.hostManager.common.dao.ICommonDao)
	 */
	@Autowired
	@Qualifier("RoleDao")
	@Override
	public void setCommonDao(ICommonDao<Role> commonDao)
	{
		this.commonDao = commonDao;
		this.roleDao = (RoleDao) commonDao;
	}
	 public Role createRole(Role role) {
	        return roleDao.createRole(role);
	    }

	    public Role updateRole(Role role) {
	        return roleDao.updateRole(role);
	    }

	    public void deleteRole(Long roleId) {
	        roleDao.deleteRole(roleId);
	    }

	    
	    public Role findOne(Long roleId) {
	        return roleDao.findOne(roleId);
	    }

	    
	    public List<Role> findAll() {
	        return roleDao.findAll();
	    }

	    
	    public Set<String> findRoles(Long... roleIds) {
	        Set<String> roles = new HashSet<String>();
	        for(Long roleId : roleIds) {
	            Role role = findOne(roleId);
	            if(role != null) {
	                roles.add(role.getRole());
	            }
	        }
	        return roles;
	    }

	    
	    public Set<String> findPermissions(Long[] roleIds) {
	        Set<Long> resourceIds = new HashSet<Long>();
	        for(Long roleId : roleIds) {
	            Role role = findOne(roleId);
	            if(role != null) {
	                resourceIds.addAll(role.getResourceIds());
	            }
	        }
	        return resourceService.findPermissions(resourceIds);
	    }
		/* (non-Javadoc)
		 * @see com.youth.fund.hk.hostManager.common.service.impl.CommonService#generateHtml(java.io.Serializable)
		 */
		@Override
		public void generateHtml(Role model)
		{
			// TODO Auto-generated method stub
			
		}
}
